angular.module('app', ['ui.router', 'oc.lazyLoad'])
    .run(['$rootScope', '$state', '$stateParams',
        function ($rootScope, $state, $stateParams) {
            $rootScope.state = $state;
            $rootScope.stateParams = $stateParams;
        }
    ])
    .config([
        '$ocLazyLoadProvider',
        '$stateProvider',
        '$urlRouterProvider',

        function ($ocLazyLoadProvider, $stateProvider, $urlRouterProvider) {
            // For any unmatched url, redirect to /
            $urlRouterProvider.otherwise('/');

            $stateProvider
                .state('app', {
                    url: '/',
                    views: {
                        header: {
                          template:
                          '<nav class="navbar navbar-default">' +
                            '<div class="container-fluid">' +
                              '<ul class="nav navbar-nav">' +
                                '<li><a ui-sref="app">Home</a></li>' +
                                '<li><a ui-sref="app.posts">Posts</a></li>' +
                              '</ul>' +
                            '</div>' +
                          '</nav>'
                        },
                        content: {
                            template: '<h3>Home view</h3>'
                        }
                    }
                })
                .state('app.posts', {
                    url: 'posts',
                    views: {
                        'content@': {
                            templateUrl: 'posts.html',
                            controller: 'PostsController',
                            resolve: {
                                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                                    return $ocLazyLoad.load({
                                        serie: true,
                                        files: [
                                            'posts.js',
                                            'posts-controller.js'
                                        ]
                                    });
                                }]
                            }
                        }
                    }
                });
        }
    ]);
